package com.florent37.runtimepermission3.slice;

import com.florent37.runtimepermission3.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.security.permission.Permission;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

public class MainAbilitySlice extends AbilitySlice {

    private Reference<Ability> activityReference;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_runtime_permissions_activity_main);

        this.activityReference = new WeakReference<Ability>(getAbility());

        Button button1 = (Button)findComponentById(ResourceTable.Id_java8);
        Button button2 = (Button)findComponentById(ResourceTable.Id_java7);
        Button button3 = (Button)findComponentById(ResourceTable.Id_openSettings);

        button1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent newIntent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.florent37.runtimepermission3")
                        .withAbilityName("com.florent37.runtimepermission3.RuntimePermissionMainActivityJava8")
                        .build();
                newIntent.setOperation(operation);
                startAbility(newIntent);
            }
        });

        button2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent newIntent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.florent37.runtimepermission3")
                        .withAbilityName("com.florent37.runtimepermission3.RuntimePermissionMainActivityJava8")
                        .build();
                newIntent.setOperation(operation);
                startAbility(newIntent);
            }
        });

        button3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                final Ability fractionAbility = activityReference.get();
                if (fractionAbility != null) {
                    Intent intent = new Intent();
                    intent.setAction(IntentConstants.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
                    intent.setUri(Uri.getUriFromParts("package", fractionAbility.getBundleName(), null));
                    fractionAbility.startAbility(intent);
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
